//
//  CircleSecondCollectionViewCell.m
//  Beauty
//
//  Created by Snow on 16/9/6.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "CircleSecondCollectionViewCell.h"
#import "WishesListCollectionViewCell.h"
#import "GXMacro.h"
#import "GXNetWorkingParser.h"
#import "ModelOfWishes.h"
@interface CircleSecondCollectionViewCell ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) NSMutableArray *arrayOfWishes;
@end
@implementation CircleSecondCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self handleData];
        _layout = [[UICollectionViewFlowLayout alloc] init];
        _layout.minimumLineSpacing = 5;
        _layout.minimumInteritemSpacing = 5;
        _layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:_layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [self.contentView addSubview:_collectionView];
        [_collectionView registerClass:[WishesListCollectionViewCell class] forCellWithReuseIdentifier:@"WishesListCollectionViewCell"];
        [self createFresh];
    }
    return self;
}

- (void)createFresh
{
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
    }];
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(handleLoadNewData)];
}
- (void)handleLoadNewData {
    //请求服务器更新数据
    _arrayOfWishes = [NSMutableArray array];
    NSUserDefaults *userdefault = [NSUserDefaults standardUserDefaults];
    NSString *password = [userdefault objectForKey:@"password"];
    if (password.length == 0) {
        if (self.delegate != nil && [self.delegate respondsToSelector:@selector(wishBackToVC)]) {
            [self.delegate wishBackToVC];
        }
    } else {
        NSString *url = [NSString stringWithFormat:@"http://www.wantease.com/mobile/index.php?act=member_favorites&op=favorites_list&key=%@&page=100", password];
        [GXNetWorkingParser GETWithURL:url par:nil success:^(id responseObject) {
            NSDictionary *dic = responseObject[@"datas"];
            NSArray *arr = dic[@"favorites_list"];
            for (NSDictionary *dict in arr) {
                ModelOfWishes *model = [[ModelOfWishes alloc] init];
                [model setValuesForKeysWithDictionary:dict];
                [_arrayOfWishes addObject:model];
            }
            [_collectionView reloadData];
        } filed:^(NSError *error) {
            
        }];
    }
    [self.collectionView.mj_header endRefreshing];
}

- (void)handleData
{
    _arrayOfWishes = [NSMutableArray array];
    NSUserDefaults *userdefault = [NSUserDefaults standardUserDefaults];
    NSString *password = [userdefault objectForKey:@"password"];
    if (password.length == 0) {
        if (self.delegate != nil && [self.delegate respondsToSelector:@selector(wishBackToVC)]) {
            [self.delegate wishBackToVC];
        }
    } else {
        NSString *url = [NSString stringWithFormat:@"http://www.wantease.com/mobile/index.php?act=member_favorites&op=favorites_list&key=%@&page=100", password];
        [GXNetWorkingParser GETWithURL:url par:nil success:^(id responseObject) {
        NSDictionary *dic = responseObject[@"datas"];
        NSArray *arr = dic[@"favorites_list"];
        for (NSDictionary *dict in arr) {
            ModelOfWishes *model = [[ModelOfWishes alloc] init];
            [model setValuesForKeysWithDictionary:dict];
            [_arrayOfWishes addObject:model];
        }
        [_collectionView reloadData];
    } filed:^(NSError *error) {
        
    }];
    }
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    _layout.itemSize = CGSizeMake((self.contentView.bounds.size.width - 20) / 3, 120);
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView.mas_left);
        make.right.mas_equalTo(self.contentView.mas_right);
        make.top.mas_equalTo(self.contentView.mas_top);
        make.bottom.mas_equalTo(self.contentView.mas_bottom);
    }];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _arrayOfWishes.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    WishesListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WishesListCollectionViewCell" forIndexPath:indexPath];
    ModelOfWishes *model = [_arrayOfWishes objectAtIndex:indexPath.row];
    [cell.image sd_setImageWithURL:[NSURL URLWithString:model.goods_image_url]];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ModelOfWishes *model = [_arrayOfWishes objectAtIndex:indexPath.row];
    if (self.delegate != nil && [self.delegate respondsToSelector:@selector(wishesWithGood_id:)]) {
        [self.delegate wishesWithGood_id:model.goods_id];
    }
}
@end
